<template>
  <div>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix box-title">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible=true">添加分类</el-button>
      </div>
      <!-- 分类的表格区域 -->
      <el-table
        :data="list" border stripe
        style="width: 100%">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column
          prop="cate_name"
          label="分类名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="cate_alias"
          label="分类别名"
          width="180">
        </el-table-column>
        <el-table-column
          label="操作">
          <template v-slot:default="obj">
          <el-button size="mini" type="primary" @click="showEdit(obj.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="remove(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
    </el-table>
    </el-card>

    <!-- 添加文章弹框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="addVisible"
      width="30%" @closed="onAddClosed"
      >
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addFormRef">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="addCate" size="mini">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改对话框 -->
    <el-dialog
      title="修改分类"
      :visible.sync="editVisible"
      width="35%"
  >
      <!-- 修改的表单 -->
      <el-form :model="editForm" label-width="80px" :rules="addFormRules" ref="editFormRef" @closed="onEditClosed">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
      <el-button @click="editVisible = false" size="mini">取 消</el-button>
      <el-button type="primary" @click="updateCate" size="mini" >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCateListAPI, addCateAPI, updateCateAPI, deleteCateAPI } from '@/api/artCate.js'
export default {
  name: 'ArtList',
  created () {
    this.initList()
  },
  data () {
    return {
      list: [],
      addVisible: false, // 添加对话框
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addFormRules: {
        cate_name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须是1-10位的非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须1-15位的字母和数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async initList () {
      const { data: res } = await getCateListAPI()
      if (res.code !== 0) return this.$message.error(res.message)
      this.list = res.data
    },
    onAddClosed () {
      this.$refs.addFormRef.resetFields()
    },
    addCate () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCateAPI(this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.addVisible = false
        this.initList()
      })
    },
    showEdit (obj) {
      if (obj.id === 1 || obj.id === 2) return this.$message.error('该条数据不可修改')
      // console.log(obj)
      this.editForm = { ...obj }
      this.editVisible = true
    },
    onEditClosed () {
      this.$refs.editFormRef.resetFields()
    },
    updateCate () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateCateAPI(this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.editVisible = false
        this.initList()
      })
    },
    remove (id) {
      if (id === 1 || id === 2) return this.$message.error('该条数据不允许删除')
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await deleteCateAPI(id)
        this.initList()
        this.$message({
          type: 'success',
          message: res.message
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.box-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
